/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_host_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleHostStub::SleHostStub()
{
    memberFuncMap_[SleHostInterfaceCode::SLE_REGISTER_HOST_CALLBACK] =
        &SleHostStub::RegisterHostCallbackInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_UNREGISTER_HOST_CALLBACK] =
        &SleHostStub::UnregisterHostCallbackInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_ENABLE] =
        &SleHostStub::EnableSleInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_DISABLE] =
        &SleHostStub::DisableSleInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_GET_SLE_STATE] =
        &SleHostStub::GetSleStateInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_GET_HOST_ADDRESS] =
        &SleHostStub::GetHostAddressInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_SET_HOST_NAME] =
        &SleHostStub::SetHostNameInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_GET_HOST_NAME] =
        &SleHostStub::GetHostNameInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_GET_DEFAULT_CONN_CONFIG] =
        &SleHostStub::GetHostDefaultConnectConfigInner;
    memberFuncMap_[SleHostInterfaceCode::SLE_GET_SLE_SERVER_REMOTE] =
        &SleHostStub::GetSleServerRemoteInner;
}

SleHostStub::~SleHostStub(){}

int SleHostStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    std::u16string descriptor = SleHostStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleHostInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleHostStub::RegisterHostCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("RegisterHostCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read host remote failed!");
    const sptr<ISleHostCallback> callback = OHOS::iface_cast<ISleHostCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read host callback failed!");

    int32_t result = RegisterHostCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleHostStub::UnregisterHostCallbackInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("UnregisterHostCallbackInner In");
    sptr<IRemoteObject> remote = data.ReadRemoteObject();
    SLE_ASSERT_AND_RETURN_RET_LOG(remote, TRANSACTION_ERR, "read host remote failed!");
    const sptr<ISleHostCallback> callback = OHOS::iface_cast<ISleHostCallback>(remote);
    SLE_ASSERT_AND_RETURN_RET_LOG(callback, TRANSACTION_ERR, "read host callback failed!");

    int32_t result = UnregisterHostCallback(callback);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleHostStub::EnableSleInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("EnableSleInner In");
    int32_t result = EnableSle();
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleHostStub::DisableSleInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("DisableSleInner In");
    int32_t result = DisableSle();
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleHostStub::GetSleStateInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetSleStateInner In");
    int32_t state = SLE_HOST_STATE_ERROR;
    int32_t result = GetSleState(state);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(state), TRANSACTION_ERR, "write sle state failed!");
    return NO_ERROR;
}

int SleHostStub::GetHostAddressInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetHostAddressInner In");
    SleAddress address;
    int32_t result = GetHostAddress(address);

    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteParcelable(&address), TRANSACTION_ERR, "write address failed!");
    return NO_ERROR;
}

int SleHostStub::SetHostNameInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("SetHostNameInner In");
    std::string name = data.ReadString();
    int32_t result = SetHostName(name);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    return NO_ERROR;
}

int SleHostStub::GetHostNameInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetHostNameInner In");
    std::string name;
    int32_t result = GetHostName(name);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteString(name), TRANSACTION_ERR, "write host name failed!");
    return NO_ERROR;
}

int SleHostStub::GetHostDefaultConnectConfigInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetHostNameInner In");
    SleConnectConfig hostConnParam;
    int32_t result = GetHostDefaultConnectConfig(hostConnParam);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(result), TRANSACTION_ERR, "write result failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(hostConnParam.connectInterval), TRANSACTION_ERR, "write connectInterval failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(hostConnParam.connectTimeout), TRANSACTION_ERR, "write connectTimeout failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(hostConnParam.connectLatency), TRANSACTION_ERR, "write connectLatency failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteInt32(hostConnParam.mtuSize), TRANSACTION_ERR, "write mtuSize failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(hostConnParam.mcs), TRANSACTION_ERR, "write mcs failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(hostConnParam.pilotDensity), TRANSACTION_ERR, "write pilotDensity failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(hostConnParam.formatType), TRANSACTION_ERR, "write formatType failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint16(hostConnParam.dataLen), TRANSACTION_ERR, "write dataLen failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteUint8(hostConnParam.phy), TRANSACTION_ERR, "write phy failed!");
    return NO_ERROR;
}

int SleHostStub::GetSleServerRemoteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("GetSleServerRemoteInner In");
    std::string name = data.ReadString();
    sptr<IRemoteObject> result = GetSleServerRemote(name);
    SLE_ASSERT_AND_RETURN_RET_LOG(reply.WriteStrongParcelable(result), TRANSACTION_ERR, "write server remote failed!");
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS